<?php

class User extends Controller{

	function user()
	{
		parent::Controller();
		$this->load->helper('url');
    $this->load->library('session');
		$this->load->model('user_model');
	}
	
  function list_users()
  {
    $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
           $this->load->helper('flexigrid');
                  
            $displayform ='';
            $displayform .='<div id=dashboard ><span>Manage Users</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
            $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><span id="create-state" style="cursor:pointer;color:blue;" >Add New User</span>&nbsp;<a href="'.base_url().'idea_home/logout">Logout</a></div>';
                       
            $displayform .='<br/>';
			
                       //here model comes....
             
          $displayform .= '<div id="dialog-form" title="Create New User">';
          $displayform .= '<p class="validateTips">All fields must be entered!!</p>';
          $displayform .= '<form id=form>';
          $displayform .= '<table><tr>';
          $displayform .= '<td align=right><label class="label" for=user">User Name:</label></td>';
          $displayform .= '<td left><input type="text" name="user_name" id="user_name" class="text ui-widget-content ui-corner-all" /></td>';
          $displayform .= '</tr>';
          $displayform .= '<tr>';
          $displayform .= '<td align=right>';
          $displayform .= '<label for="email" class="label" >Email Address:</label>';
          $displayform .= '</td>';
          $displayform .= '<td align=left>';
          $displayform .= '<input type="text" name="email" id="email" class="text ui-widget-content ui-corner-all" />';
          $displayform .= '</td>';
          $displayform .= '</tr>';
          $displayform .= '<tr>';
          $displayform .= '<td align=right>';
          $displayform .= '<label for="phone" class="label" >Phone Number:</label>';
          $displayform .= '</td>';
          $displayform .= '<td align=left>';
          $displayform .= '<input type="text" name="phone_num" id="phone_num" class="text ui-widget-content ui-corner-all" />';
          $displayform .= '</td>';
          $displayform .= '</tr>';
          $displayform .= '<tr>';
          $displayform .= '<td align=right>';
          $displayform .= '<label for="cities" class="label" >City:</label>';
          $displayform .= '</td>';
          $displayform .= '<td align=left>';
          $city = $this->user_model->fetch_city();
          $displayform .= '<select name=city id="city" class="text ui-widget-content" style="width:245px;" >';
          $displayform .= '<option value="" >-Please Select City-</option>';
          foreach($city->result_array() as $row)
          {
            $displayform .= '<option value='.$row['city_id'].'>'.$row['city_name'].'</option>';
          }
          $displayform .= '</select>';
          $displayform .= '</td>';
          $displayform .= '</tr>';
          $displayform .= '<tr>';
          $displayform .= '<td align=right>';
          $displayform .= '<label for="landline" class="label" >LandLine:</label>';
          $displayform .= '</td>';
          $displayform .= '<td align=left>';
          $displayform .= '<input type="text" name="landline" id="landline" class="text ui-widget-content ui-corner-all" />';
          $displayform .= '</td>';
          $displayform .= '</tr>';
          $displayform .= '<tr>';
          $displayform .= '<td align=right>';
          $displayform .= '<label for="loc_addr" class="label" >Local Address:</label>';
          $displayform .= '</td>';
          $displayform .= '<td align=left>';
          $displayform .= '<textarea name="loc_addr" id="loc_addr" cols="18" rows="4" class="text ui-widget-content ui-corner-all" /></textarea>';
          $displayform .= '</td>';
          $displayform .= '</tr>';
          $displayform .= '<tr>';
          $displayform .= '<td align=right>';
          $displayform .= '<label for="per_addr" class="label" >Permament Address:</label>';
          $displayform .= '</td>';
          $displayform .= '<td align=left>';
          $displayform .= '<textarea name="per_addr" id="per_addr" cols="18" rows="4" class="text ui-widget-content ui-corner-all" /></textarea>';
          $displayform .= '</td>';
          $displayform .= '</tr>';
          $displayform .= '</table>';
           
          
          $displayform .= '</form>';
          $displayform .= '</div>';
          //ends here....
          
            $displayform .= '<table id="flex1" style="display:none" border=0 align=left ></table>';
            $displayform .= '<br/>';
          
          $colModel['user_id'] = array('Sl No',60,TRUE,'center',0);
          $colModel['user_name'] = array('User Name',160,TRUE,'left',2);
          $colModel['>user_phone'] = array('Mobile Number',160,TRUE,'left',1);
          $colModel['user_email'] = array('Email Address',160,TRUE,'left',1);
          $colModel['user_number'] = array('Landline',160,TRUE,'left',1);
          
          $gridParams = array(
            'width' => 'auto',
            'height' => 400,
            'rp' => 15,
            'rpOptions' => '[10,20,30,40]',
            'pagestat' => 'Displaying:{from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'List of Users',
            'showTableToggleBtn' => true
          );
          
          $grid_js = build_grid_js('flex1',site_url("/ajax/user_list"),$colModel,'caf_id','asc',$gridParams);
          
          $data['js_grid'] = $grid_js;
           
            //model javasrcipt comes here...
          
           $this->template->add_css('
        
          .label, #email, #user_name,#loc_addr,#per_addr,#phone_num,#landline,#city { display:block; }
          
          
          fieldset { padding:0; border:0; margin-top:25px; }
          h1 { font-size: 1.2em; margin: .6em 0; }
         
          .ui-dialog .ui-state-error { padding: .3em; }
          .validateTips { border: 1px solid transparent; padding: 0.3em; }
              
          ','embed');
          
          
          $this->template->add_js('js/ui/jquery.ui.position.js');
          $this->template->add_js('js/ui/jquery.ui.mouse.js');
          $this->template->add_js('js/ui/jquery.ui.draggable.js');
          $this->template->add_js('js/ui/jquery.ui.resizable.js');
          $this->template->add_js('js/ui/jquery.ui.dialog.js');
          $this->template->add_js('js/ui/jquery.effects.core.js');
          $this->template->add_js('js/caf/user.js');
            
          $data = array('formdisplay'=>$displayform,'js_grid'=>$grid_js );  
          $this->template->add_css('css/flexigrid/flexigrid.css');
        //  $this->template->add_js('js/flexigride/jquery.pack.js');
          $this->template->add_js('js/flexigride/flexigrid.pack.js');
          
          //ends here....
          $this->template->write_view('header','header', $data, True);
          $this->template->write_view('content', 'general_flexigrid');
          $this->template->render();
                 
  }
  else
  {
    echo "Forbidden to Access!!";
  }           
    
  }

  function ajax_addnew_user()
  {
  //  print_r($_POST);
   
    $result = $this->user_model->insert_user();
    if($result == 0)
    {
      
      echo json_encode("The user you're trying to insert is already exist!!");
    }
    else
    {
      echo json_encode("User successfully added,please refresh your grid to see changes!!");  
    }
   
    
  }
  
  //old one not in use....
  /*
	function user_add()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Manage Users</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
		$displayform .='<fieldset style="width:50%;"><legend>Add New User</legend>';
		//variable declaration...
		$user_name = array('name'=>'user_name','id'=>'user_name',
				'type'=>'text','class'=>'required');
		$email = array('name'=>'email','id'=>'email',
				'type'=>'text','class'=>'required email');
		$addr1 = array('name'=>'addr1','id'=>'addr1','cols'=>'25','rows'=>'4',
				'type'=>'text','class'=>'required');
		$addr2 = array('name'=>'addr2','id'=>'addr2','cols'=>'25','rows'=>'4',
				'type'=>'text','class'=>'required');
		$phone = array('name'=>'phone','id'=>'phone','maxlength'=>'12',
				'type'=>'text','class'=>'required number');
		$landline = array('name'=>'landline','id'=>'landline','maxlength'=>'16','class'=>'number',
				'type'=>'text',);
		
				
		$attr = array('id'=>'new_user');
		
		$displayform .= form_open('user/user_add',$attr);
		$displayform .= '<table>';
		$displayform .= '<tr><td align=left> User Name:</td>';
		$displayform .= '<td>'.form_input($user_name).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=left> Email Address:</td>';
		$displayform .= '<td>'.form_input($email).'<div id=check_user></div></td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=left> Phone:</td>';
		$displayform .= '<td>'.form_input($phone).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=left> City:</td>';
		$displayform .= '<td>';
		$city = $this->user_model->fetch_city();
		$displayform .= '<select name=city class=required >';
		$displayform .= '<option value="" >-Please Select City-</option>';
		foreach($city->result_array() as $row)
		{
			$displayform .= '<option value='.$row['city_id'].'>'.$row['city_name'].'</option>';
		}
		$displayform .= '</select>';
		$displayform .='</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=left> Landline:</td>';
		$displayform .= '<td>'.form_input($landline).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=left> Local Address:</td>';
		$displayform .= '<td>'.form_textarea($addr1).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=left> Permanent Address:</td>';
		$displayform .= '<td>'.form_textarea($addr2).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=right colspan=2>'.form_submit('Submit','Create New User').' '.form_reset('cancel','Cancel'). '</td>';
		$displayform .= '</table>';
		$displayform .= form_close();
		$displayform .='</fieldset>';
//		validating fields....
		$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|xss_clean');
		$this->form_validation->set_rules('phone', 'Phone', 'trim|required|numeric|max_length[12]|min_length[10]|xss_clean');
		$this->form_validation->set_rules('landline', 'Land Line', 'trim|numeric|min_length[7]|xss_clean');
		$this->form_validation->set_rules('addr1', 'Local Address', 'trim|required|xss_clean');
		$this->form_validation->set_rules('addr2', 'Permanent Address', 'trim|required|xss_clean');
		$this->form_validation->set_rules('city', 'City Name', 'trim|required|xss_clean');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->template->add_js('js/jquery-latest.js');	
			$this->template->add_js('js/jquery.validate.js');
			
			$this->template->add_js('$(document).ready(function(){
	 		   $("#new_user").validate();
	  			});
		','embed');
			$this->template->add_js('	
			$(document).ready(function(){
			$("").change(function(){
				var src = $(this).val();
				alert(src);
			 	$.ajax({
			type: "POST",
			url: "/user/check_user/",

			dataType: "text/html",
			success: function(html){       $("#check_user").html(html);     }

			  });

			});
			});
			','embed');
			
			$data = array('formdisplay'=>$displayform );	
			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'general');
			$this->template->render();
		}
		else
		{
			
			$this->user_model->insert_user();
			redirect('user/list_users/0');
		}
	}
   * 
   
	//ajax checking user...
	function check_user()
	{
		print_r($_POST);
	}
   **/
//duplicate user...
	
//to list user details....
	
/*	function list_users($id=0)
	{
		$this->load->library('pagination');
		
		//pagingation....
		$config['base_url'] = base_url().'index.php/users/list_users';
		$config['total_rows'] = $this->user_model->users_count(); 
		$config['per_page'] = '10';
		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';
		$this->pagination->initialize($config);
	  	$display_users = $this->user_model->list_users($id,$config['per_page']);
	  	
	  	$displayform ='';
	  	$displayform .='<div id=dashboard ><span>Manage Users</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
	  	$displayform .='<div align=right><a href='.base_url().'user/user_add >[Add New User]</a></div>';
	  	$displayform .='<br/>';
	  	$displayform .='<fieldset style="width:60%;"><legend>List of Users</legend>';
	  	$displayform .= '<table width="100%"  align=center class="tablestyle" cellpadding="2" style="border:1px solid #cccccc">';
		$displayform .= '<tr>';
		$displayform .= '<th align=left>Slno</th>';
		$displayform .= '<th align=left>User Name</th>';
		$displayform .= '<th align=left>User Phone Number</th>';	
		$displayform .= '<th align=left>User Email-Id</th>';	
		$displayform .= '</tr>';
		
		if($display_users->num_rows() == 0)
			{
				$displayform .= '<tr><td colspan=11 align=center>--No results found to display!!---</td></tr>';
			}
			else
			{	
				$i=$id;
				foreach($display_users->result_array() as $row)
				{
					$i++;
				
					$displayform .= '<tr style="font-family:Times;font-size:14px;"><td align=left>'.$i.'</td>';
					$displayform .= '<td align=left><a href='.base_url().'user/user_edit/'.$row['user_id'].'> '.$row['user_name'].'</a></td><td align=left>'.$row['user_phone'].'</td><td align=left>'.$row['user_email'].'</td>';
					
				}
				$displayform .= '</tr>';
			}
		$displayform .='</table></fieldset>';	
		$displayform .= $this->pagination->create_links();
	
		$data = array('formdisplay'=>$displayform );
		$this->template->add_js('jquery-1.3.2');
		$this->template->add_js('
				$(function() {
					$(".tablestyle tr:even").addClass("odd");
					$(".tablestyle tr:odd").addClass("even");
					
				', 'embed');	
		$this->template->write_view('header','header', $data, True);
		$this->template->write_view('content', 'other_general');
		$this->template->render();		
	}
 
 */
//to edit infor...
	function user_edit($id=0)
	{
	  $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
    		$user = $this->user_model->fetch_user($id);
    		$user_info = $user->result_array();
    		
    		$this->load->helper('form');
    		$displayform ='';
    		$displayform .='<div id=dashboard ><span>Manage Users</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
        $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
        $displayform .= '<br/>';
        
    		$displayform .='<fieldset style="width:50%;"><legend>Edit User</legend>';
    		
    		$user_name = array('name'=>'user_name','id'=>'user_name',
    				'type'=>'text','class'=>'required','value'=>$user_info[0]['user_name']);
    		$email = array('name'=>'email','id'=>'email','value'=>$user_info[0]['user_email'],'type'=>'text','class'=>'required email',);
    		$addr1 = array('name'=>'addr1','id'=>'addr1','cols'=>'25','rows'=>'4','value'=>$user_info[0]['user_add1'],'type'=>'text','class'=>'required');
    		$addr2 = array('name'=>'addr2','id'=>'addr2','cols'=>'25','rows'=>'4','value'=>$user_info[0]['user_add2'],'type'=>'text','class'=>'required');
    		$phone = array('name'=>'phone','id'=>'phone','maxlength'=>'12','value'=>$user_info[0]['user_phone'],'type'=>'text','class'=>'required number');
    		$landline = array('name'=>'landline','id'=>'landline','maxlength'=>'16','class'=>'number','value'=>$user_info[0]['user_number'],'type'=>'text',);
    		
    		
    		$attr = array('id'=>'edit_user');
    		
    		$displayform .= form_open('user/save_edit',$attr);
    		$displayform .= '<input type=hidden name=user_id value='.$user_info[0]['user_id'].'>';
    		$displayform .= '<table>';
    		
    		$displayform .= '<tr><td align=left> Email Address:</td>';
    		$displayform .= '<td>'.form_input($email).'</td>';
    		$displayform .= '</tr>';
    		$displayform .= '<tr><td align=left> User Name:</td>';
    		$displayform .= '<td>'.form_input($user_name).'</td>';
    		$displayform .= '</tr>';
    		$displayform .= '<tr><td align=left> Phone:</td>';
    		$displayform .= '<td>'.form_input($phone).'</td>';
    		$displayform .= '</tr>';
    		$displayform .= '<tr><td align=left> Landline:</td>';
    		$displayform .= '<td>'.form_input($landline).'</td>';
    		$displayform .= '</tr>';$displayform .= '<tr><td align=left> City:</td>';
    		$displayform .= '<td>';
    		$city = $this->user_model->fetch_city();
    		$displayform .= '<select name=city class=required >';
    		$displayform .= '<option value='.$user_info[0]['city_id'].' >'.$user_info[0]['city_name'].'</option>';
    		foreach($city->result_array() as $row)
    		{
    			$displayform .= '<option value='.$row['city_id'].'>'.$row['city_name'].'</option>';
    		}
    		$displayform .= '</select>';
    			
    		$displayform .= '</td>';
    		
    		$displayform .= '</tr>';
    		$displayform .= '<tr><td align=left> Local Address:</td>';
    		$displayform .= '<td>'.form_textarea($addr1).'</td>';
    		$displayform .= '</tr>';
    		$displayform .= '<tr><td align=left> Permanent Address:</td>';
    		$displayform .= '<td>'.form_textarea($addr2).'</td>';
    		$displayform .= '</tr>';
    		$displayform .= '<tr><td align=right colspan=2>'.form_submit('Submit','Update User Info').' '.form_reset('cancel','Cancel'). '</td>';
    		$displayform .= '</table>';
    		$displayform .= form_close();
    		$displayform .='</fieldset>';
    		
    		$this->template->add_js('js/jquery-latest.js');	
    			$this->template->add_js('js/jquery.validate.js');
    			
    			$this->template->add_js('$(document).ready(function(){
    	 		   $("#edit_user").validate();
    	  			});
    		','embed');
    			$data = array('formdisplay'=>$displayform );	
    			$this->template->write_view('header','header', $data, True);
    			$this->template->write_view('content', 'general');
    			$this->template->render();
    	}
  else
  {
    echo "Forbidden to access!!";
  }
		
	}
	
	function save_edit()
	{
		$user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
		    $check = $this->user_model->save_edit();
		    redirect('user/list_users/0');
		}
    else
      {
        echo "Forbidden to access";
      }
	}
		
}
